﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage0 : Stage
    {

        public Stage0(Game game) : base(game) { }

        public List<GroundObject> groundList = new List<GroundObject>();
        float GROUND_ALPHA = 0f;
        ConsoleMechine console;
        public override void InitializeScenes()
        {
            base.InitializeScenes();
            Animation a = new Animation("images/stages/root/root0");
            a.LayerDepth = 0.9f;_backGround.Add(a);
            a = new Animation("images/stages/root/root1");
            a.LayerDepth = 0.9f;_backGround.Add(a);

            _backGroundPos = new Vector2(-435, -400);
            _scenceBound = new Rectangle(400, 180, 600, 600);
            camera.Bound = _scenceBound;
           // basePos = player.Position;

            for (int i = 0; i < 2; i++)
            {
                Animation wallBack = new Animation("images/stages/stage0/stage0_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(-50, -318);

            if (StageManager.Save.MechineList.Contains("Stage00_Console_1")) { }

            Transition tra;
            tra = new DefauldTransition( 1, 0, 0, 60);
            tra.Register(this);
            ProtalGate protalSwitch;
            
            GroundObject grount;
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(321, 485); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(93, 18)); grount.Size = new Vector2(90, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //0
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(414, 485); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(93, 18)); grount.Size = new Vector2(90, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //1
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(301, 503); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(133, 18)); grount.Size = new Vector2(130, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //2
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(434, 503); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(133, 18)); grount.Size = new Vector2(130, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //3
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(281, 521); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(173, 18)); grount.Size = new Vector2(170, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //4
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(454, 521); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(173, 18)); grount.Size = new Vector2(170, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //5
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(261, 539); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(213, 18)); grount.Size = new Vector2(210, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //6
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(474, 539); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(213, 18)); grount.Size = new Vector2(210, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //7


            grount = new GroundObject(this._world, this); grount.Position = new Vector2(241, 557); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(253, 18)); grount.Size = new Vector2(250, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //8
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(494, 557); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(253, 18)); grount.Size = new Vector2(250, 20); grount.Rotation = 0f; Actors.Add(grount);
            groundList.Add(grount); //9



            grount = new GroundObject(this._world, this); grount.Position = new Vector2(901, 611); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(1024, 90)); grount.Size = new Vector2(1024, 90); grount.Rotation = 0f; grount.Alpha = GROUND_ALPHA; Actors.Add(grount);
           
            protalSwitch = new ProtalGate("Machine_Protal1", _world, this); protalSwitch.ProtalCommand = new ProtalCommand("rootStage", "rootProtal2"); protalSwitch.Position = new Vector2(1000, 517); Actors.Add(protalSwitch);

            grount = new GroundObject(this._world, this); grount.Position = new Vector2(1375, 367); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(75, 400)); grount.Size = new Vector2(75, 400); grount.Rotation = 0f; grount.Alpha = GROUND_ALPHA; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(29, 366); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(75, 400)); grount.Size = new Vector2(75, 400); grount.Rotation = 0f; grount.Alpha = GROUND_ALPHA; Actors.Add(grount);
            grount = new GroundObject(this._world, this); grount.Position = new Vector2(190, 611); grount.CurrentAnimation = new Animation("images/ground/dirtwall", new Point(398, 90)); grount.Size = new Vector2(398, 90); grount.Rotation = 0f; grount.Alpha = GROUND_ALPHA; Actors.Add(grount);
            //grount = new GroundObject(this._world, this); grount.Position = new Vector2(368, 436); grount.CurrentAnimation = new Animation("images/system/black", new Point(92, 80)); grount.Size = new Vector2(92, 80); grount.Rotation = 0f; Actors.Add(grount);

            //protalSwitch =
            //StateFactory.CreatePortal("Machine_Protal2", this, 373, 530, "stage6", "Stage6_Portal_1");
            //protalSwitch.CurrentAnimation = new Animation("images/items/protalGate", new Point(60, 80)); 
            //protalSwitch.CurrentAnimation.LayerDepth = 0.95f;

            protalSwitch = new ProtalGate("Machine_Protal2", this.World, this,new Vector2(60,80));
            protalSwitch.ProtalCommand = new ProtalCommand("stage6", "Stage6_Portal_1");
            protalSwitch.Position = new Vector2(373, 520);
            protalSwitch.CurrentAnimation.LayerDepth = 0.95f;
            Actors.Add(protalSwitch);


            if (!StageManager.Save.MechineList.Contains("Stage00_Console_1"))//not act
            {
                console = new ConsoleMechine("Stage00_Console_1", _world, this);
                console.Position = new Vector2(368, 420); Actors.Add(console);
            }
            else { //
                for (int i = 0; i <= 4; i++)
                {
                    //ModifierContain first = new ModifierContain(groundList[i * 2]);
                    //PositionModifier m2 = new PositionModifier(groundList[i * 2].Position, groundList[i * 2].Position + new Vector2(-80, 0), (i * 60), (i * 60) + 60);
                    //first.AddModifier(m2);
                    groundList[i * 2].Position += new Vector2(-80, 0);
                }
                for (int i = 0; i <= 4; i++)
                {
                    //ModifierContain first = new ModifierContain(groundList[i * 2 + 1]);
                    //PositionModifier m2 = new PositionModifier(groundList[i * 2 + 1].Position, groundList[i * 2 + 1].Position + new Vector2(80, 0), (i * 60), (i * 60) + 60);
                    //first.AddModifier(m2);
                    groundList[i * 2 + 1].Position += new Vector2(80, 0);
                }

            }
            
            if (StageManager.Save.MechineList.Count() < 3)
            {
                _song = MusicManager.LoadSong("Music/stageroot");
                
                if (GameClass._musicString == "Music/stageroot")
                { }
                else
                {
                    GameClass._musicString = "Music/stageroot";
                    MediaPlayer.Play(_song);
                }
            }
            else
            {
                _song = MusicManager.LoadSong("Music/tamhe01");
                GameClass._musicString = "Music/tamhe01";
                MediaPlayer.Play(_song);
            }

            MediaPlayer.IsRepeating = true;
           // MediaPlayer.Volume = 1f;

            //mechine.Capacity = 1;
            //mechine.AddTarget(grount);
        }

        public void OpenStone()
        {
            console.Position = new Vector2(2000, 420);//移走
            this.LockTouch = false;
            for (int i = 0; i <= 4; i++)
            {
                ModifierContain first = new ModifierContain(groundList[i*2]);
                PositionModifier m2 = new PositionModifier(groundList[i * 2].Position, groundList[i * 2].Position + new Vector2(-80, 0), (i * 60), (i * 60)+60);
                first.AddModifier(m2);
                groundList[i * 2].ModifierContainer = first;
            }
            for (int i = 0; i <= 4; i++)
            {
                ModifierContain first = new ModifierContain(groundList[i * 2 + 1]);
                PositionModifier m2 = new PositionModifier(groundList[i * 2 + 1].Position, groundList[i * 2 + 1].Position + new Vector2(80, 0), (i * 60), (i * 60) + 60);
                first.AddModifier(m2);
                groundList[i * 2 + 1].ModifierContainer = first;
            }


 
        }


        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {
            base.OnDraw(spriteBatch, delta);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
           // basePos = player.Position;
            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();
            DrawWalls(spriteBatch);
            //spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "(" + _scenceBound.Left + "," + _scenceBound.Top + ")" + "(" + _scenceBound.Right + "," + _scenceBound.Bottom + ")", new Vector2(50, 50), Color.Black);
            //spriteBatch.End();

        }

        public override Stage Clone()
        {
            Stage0 stage = new Stage0(Game);
            stage.InitializeScenes();
            return stage;
        }
    }
}
